const {readdirSync} = require('fs')
const { getProperty,assert, each } = require('../../lib/tools')
const { readYaml } = require('../../lib/files')
const { join } = require('path')

/**
 * 将controller、service等实体信息写出到writeOutTheArea
 * @param {*} param0 
 */
module.exports = function({config:{globeConfig:{globe}},writeOutTheArea, classes: {controller, service, serviceInter, mapper}}){
    // 根据配置目录的配置文件进行循环迭代
    const dirPath = getProperty(globe,g=>g.dist.backend.configPath,undefined)
    assert(dirPath,"前端配置路径不能为空")
    const files = getProperty(globe,g=>readdirSync(g.dist.backend.configPath),undefined)
    if(!files){
        console.error("前端配置文件目录错误!")
    }
    
    const types = {'class':1,'interface':1}
    const checkType = c=> c.type && c.type in types
    files.forEach(f=>{
        // 读入前端页面的配置
        const filePath = join(dirPath,f)

        const content = getProperty(filePath,f=> readYaml(f),undefined)

        if(content && checkType(content)){
            content.format = 'java'
            writeOutTheArea.push(content)
        }

    })
    each(controller,c=>{
        writeOutTheArea.push(c)
    })

    each(service,s=>{
        writeOutTheArea.push(s)
    })

    each(serviceInter,s=>{
        writeOutTheArea.push(s)
    })
       
    each(mapper,s=>{
        writeOutTheArea.push(s)
    })
       
}


